package cn.bone.server.config;

import cn.bone.server.common.GlobalParams;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * Created by superbone on 2017/4/6.
 */
@Configuration
public class MysqlConfig {

    @Resource
    private GlobalParams globalParams;

    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(globalParams.getMysql().getUrl());
        dataSource.setUsername(globalParams.getMysql().getUsername());
        dataSource.setPassword(globalParams.getMysql().getPassword());
        dataSource.setDriverClassName(globalParams.getMysql().getDriverClassName());
        dataSource.setInitialSize(1);
        dataSource.setMinIdle(1);
        dataSource.setMaxActive(5);
        //配置获取连接等待超时时间
        dataSource.setMaxWait(5000);
        //配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        //配置一个连接在池中最小生存的时间，单位是毫秒
        dataSource.setMinEvictableIdleTimeMillis(300000);
        dataSource.setValidationQuery("SELECT 1 FROM DUA");
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);
        return dataSource;
    }
}
